package com.project.psc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.psc.bean.OrderBean;
import com.project.psc.bean.SupplyBean;
import com.project.psc.dto.SupplierDto;
import com.project.psc.service.IOrderService;
import com.project.psc.util.SupplierThreadLocal;
import com.project.psc.vo.SupplyVo;
import com.project.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @GetMapping("cutByItem")
    public ResultVo cutByItem(String pact, LocalDate startTime, LocalDate endTime, Integer pageNO){
        SupplierDto loginUser = SupplierThreadLocal.getSupplier();
        IPage<OrderBean> orderBeanIPage = orderService.cutByItem(loginUser.getId(), pact, startTime, endTime, pageNO);
        return ResultVo.ok(orderBeanIPage);
    }

    @GetMapping("findById")
    public ResultVo findById(Long id){
        OrderBean byId = orderService.findById(id);
        return ResultVo.ok(byId);
    }
    @GetMapping("findSupplyList")
    public ResultVo findSupplyList(String orderId){
        List<SupplyVo> supplyList = orderService.findSupplyList(orderId);
        return ResultVo.ok(supplyList);
    }
    @GetMapping("add")
    public ResultVo add(String pact){
        SupplierDto loginUser = SupplierThreadLocal.getSupplier();
        boolean add = orderService.add(loginUser.getId(), loginUser.getSupplierName(), pact);

        if (add){
            return ResultVo.ok();
        }
        return ResultVo.fail("该订单已存在");
    }


}
